<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生名称" prop="studentName" v-if="!isStudentRole">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导师名称" prop="teacherName"  v-if="!isStudentRole">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入导师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-if="!isStudentRole">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sxcg:sxcj:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sxcg:sxcj:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sxcg:sxcj:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sxcjList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50" type="index" />
      <el-table-column label="学生" align="center" prop="studentName" />
      <el-table-column label="导师" align="center" prop="teacherName" />
      <el-table-column label="实习成绩" align="center" prop="sxcj" />
      <el-table-column label="录入时间" align="center" prop="lrsj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lrsj, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sxcg:sxcj:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sxcg:sxcj:remove']"
          >删除</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['sxcg:sxcj:view']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改实习成绩对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" inline>
        <el-form-item label="导师" prop="teacherId">
          <el-select v-model="form.teacherId" placeholder="请选择导师" filterable clearable disabled style="width: 240px;">
            <el-option
              v-for="item in teacherSelectData"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="学生" prop="studentId">
          <el-select v-model="form.studentId" placeholder="请选择学生" filterable clearable style="width: 240px;" :disabled="this.isView || this.isEditStudent">
            <el-option
              v-for="item in studentSelectData"
              :key="item.studentId"
              :label="item.studentName"
              :value="item.studentId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="实习成绩" prop="sxcj">
          <el-input-number v-model="form.sxcj"  :precision="2" :min="0" :max="100" placeholder="请输入实习成绩" :disabled="this.isView" style="width: 240px;"/>
        </el-form-item>
        <el-form-item label="实习评语" prop="sxpy">
          <el-input v-model="form.sxpy" type="textarea" placeholder="请输入内容" :disabled="this.isView" style="width: 600px;"/>
        </el-form-item>
        <el-form-item label="录入时间" prop="lrsj" v-if="this.isView">
          <el-date-picker
            clearable
            :disabled="this.isView"
            style="width: 240px"
            v-model="form.lrsj"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择录入时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="this.isView" style="width: 600px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer"  v-if="!this.isView">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSxcj, getSxcj, delSxcj, addSxcj, updateSxcj } from "@/api/sxcg/sxcj"
import {getUserSelect} from "@/api/system/user";
import {getStudentDataByTeacherId} from "@/api/xxgl/sxxx";
import dayjs from "dayjs";
export default {
  name: "Sxcj",
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 当前用户ID
      currentUserId: this.$store.state.user.id,
      // 当前用户角色
      currentUserRole: this.$store.state.user.roles[0],
      // 实习成绩表格数据
      sxcjList: [],
      // 导师下拉框数据
      teacherSelectData: [],
      // 学生下拉框数据
      studentSelectData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否禁用查看按钮
      isView: false,
      // 是否禁用编辑学生下拉框
      isEditStudent: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        teacherId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teacherId: [{ required: true, message: "请选择导师", trigger: "blur" }],
        studentId: [{ required: true, message: "请选择学生", trigger: "blur" }],
        sxcj: [{ required: true, message: "请输入实习成绩", trigger: "blur" }],
        sxpy: [{ required: true, message: "请输入实习评语", trigger: "blur" }],
        lrsj: [{ required: true, message: "请选择录入时间", trigger: "blur" }],
      }
    }
  },
  created() {
    this.getList()
    this.getUserSelect()
  },
  computed:{
    // 判断当前是否为学生角色
    isStudentRole() {
      return this.currentUserRole === 'student';
    },
  },
  methods: {
    /** 查询实习成绩列表 */
    getList() {
      this.loading = true
      if(this.currentUserRole === 'student'){
        this.queryParams.glId = this.currentUserId
      }else if(this.currentUserRole === 'teacher'){
        this.queryParams.teacherId = this.currentUserId
      }
      listSxcj(this.queryParams).then(response => {
        this.sxcjList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        sxcjId: null,
        studentId: null,
        teacherId: null,
        sxcj: null,
        sxpy: null,
        lrsj: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    getUserSelect(){
      /*  roleId  2:学生  3:老师 */
      /* 查询角色为老师下拉框内容*/
      getUserSelect(3).then(response => {
        this.teacherSelectData = response.data
      })
      getStudentDataByTeacherId(this.currentUserId).then(response => {
        this.studentSelectData = response.data
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sxcjId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.isView = false
      this.isEditStudent = false
      if(this.currentUserRole === 'teacher'){
        this.form.teacherId = this.currentUserId
        this.open = true
      }else {
        this.$modal.msgError("当前登录用户角色不是导师，不能添加实习成绩")
      }
      this.title = "添加"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const sxcjId = row.sxcjId || this.ids
      getStudentDataByTeacherId(row.teacherId).then(response => {
        this.studentSelectData = response.data
      })
      getSxcj(sxcjId).then(response => {
        this.form = response.data
        this.isView = false
        this.open = true
        this.isEditStudent = true
        this.title = "修改"
      })
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset()
      const sxcjId = row.sxcjId || this.ids
      getStudentDataByTeacherId(row.teacherId).then(response => {
        this.studentSelectData = response.data
      })
      getSxcj(sxcjId).then(response => {
        this.form = response.data
        this.isView = true
        this.open = true
        this.title = "查看"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sxcjId != null) {
            this.form.lrsj = this.$dayjs().format('YYYY-MM-DD HH:mm:ss')
            updateSxcj(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            this.form.lrsj = this.$dayjs().format('YYYY-MM-DD HH:mm:ss')
            addSxcj(this.form).then(response => {
              this.$modal.msgSuccess("录入成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sxcjIds = row.sxcjId || this.ids
      this.$modal.confirm('是否确认删除?').then(function() {
        return delSxcj(sxcjIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sxcg/sxcj/export', {
        ...this.queryParams
      }, `实习成绩${this.$dayjs().format('YYYY-MM-DD HH:mm:ss')}.xlsx`)
    }
  }
}
</script>
